package com.wash.shoes.controller.pc;


import cn.hutool.core.thread.ThreadUtil;
import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Assert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.spring.SpringUtils;
import com.wash.shoes.common.enums.OrderStatusEnum;
import com.wash.shoes.domain.dto.DyRefundDto;
import com.wash.shoes.domain.dy.request.DyRefundRequest;
import com.wash.shoes.domain.jd.JdOrderService;
import com.wash.shoes.entity.CustomerPo;
import com.wash.shoes.entity.DyRefundPo;
import com.wash.shoes.entity.OrderPo;
import com.wash.shoes.entity.OrderTransportPo;
import com.wash.shoes.exception.WashException;
import com.wash.shoes.service.CustomerService;
import com.wash.shoes.service.DyRefundService;
import com.wash.shoes.service.OrderService;
import com.wash.shoes.service.OrderTransportService;
import com.wash.shoes.util.CurrenciesUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.Date;
import java.util.Objects;
import java.util.concurrent.Executor;

/**
 * @author zhengyunpeng
 * @since 2024-08-27
 */
@Api(tags = {"PC-抖音退款日志"})
@RestController
@RequestMapping( "/refund")
public class DyRefundController {

    @Autowired
    DyRefundService dyRefundService;

    @Autowired
    OrderService orderService;

    @Autowired
    DyRefundRequest dyRefundRequest;

    @Autowired
    CustomerService customerService;

    @Autowired
    OrderTransportService orderTransportService;

    @Autowired
    JdOrderService jdOrderService;


    @ApiOperation("退款列表,退款日志")
    @PostMapping("/list")
    public R<Page<DyRefundPo>> pageList(@RequestBody DyRefundDto dyRefundDto) {
        Page<DyRefundPo> page = dyRefundService.pageList(dyRefundDto);
        return R.ok(page);
    }

    @ApiOperation("发起退款 ，通过1088三方api进行退款")
    @PostMapping("/offerRefund")
    public R offerRefund(@Valid  String orderNo) {
        OrderPo orderPo = orderService.getOne(new LambdaQueryWrapper<OrderPo>().eq(StringUtils.isNotBlank(orderNo),OrderPo::getOrderNo ,orderNo));
        if(Objects.isNull(orderPo)) {
            throw new WashException("订单号不正确:"+ orderNo);
        }

        //判断状态，按照道理这里要判断订单状态是不是取鞋中，但是有极端情况用户支付了 我们的系统还是待支付，所以这里暂时取消订单状态判断
        //只判断订单来源
        if(!StringUtils.equals(orderPo.getComeFrom(),"3")) {
            throw new WashException("订单来源不正确:"+ orderPo.getComeFrom());
        }

        //取消物流,按照道理还要判断快递状态，这里给用户全部退款就不判断了
        OrderTransportPo orderTransportPo = orderTransportService.getOneByOrderNoType(orderNo, "取鞋");
        if(Objects.nonNull(orderTransportPo)) {
            jdOrderService.cancelJdOrder(orderTransportPo.getWayBillCode());
        }

        DyRefundPo dyRefundPo = new DyRefundPo();
        dyRefundPo.setJsonText("系统操作退款");
        dyRefundPo.setDyOrderId(orderPo.getMtOrderId());
        dyRefundPo.setOrderId(orderPo.getId());
        dyRefundPo.setOpenId(customerService.getById(orderPo.getCustomId()).getDyOpenId());
        dyRefundPo.setOutOrderNo(orderPo.getOrderNo());
        dyRefundPo.setRefundId("系统退款");
        dyRefundPo.setAppId("系统退款");
        JSONObject refundJson = dyRefundRequest.refund2(orderPo);
        dyRefundRequest.cancelJdOrder(orderPo); //取消物流
        dyRefundRequest.audit(orderPo); //todo 需要拿到是否需要审核，在通过审核去调用
        String errorCode = refundJson.getJSONObject("data").getString("error_code");
        String description = refundJson.getJSONObject("data").getString("description");
        dyRefundPo.setErrorCode(errorCode);
        dyRefundPo.setRemark(description);
        LoginUser loginUser = SecurityUtils.getLoginUser();
        dyRefundPo.setCreateBy(loginUser.getUser().getUserName());
        dyRefundPo.setCreateTime(new Date());
        dyRefundPo.setRefundText(refundJson.toString());
        if (errorCode.equals("0")) {
            dyRefundPo.setStatus(1);
        } else {
            dyRefundPo.setStatus(2);
            throw new WashException("退款失败"+refundJson.toJSONString());
        }
        //只有退款成功才可以进入，退款失败不能进入，如果直接调用退款的话，不会引起抖音预定订单的取消，请主动到页面取消
        dyRefundService.saveOrUpdate(dyRefundPo);

        //更新订单状态为取消
        orderPo.setStatus(OrderStatusEnum.CANCEL.getStatus());
        orderService.updateById(orderPo);
        return R.ok();
    }

    @ApiOperation("发起退款 ，通过1088三方api进行退款")
    @PostMapping("/userCancelBook")
    public R userCancelBook(@Valid  String orderNo,@Valid  String bookId) {
        OrderPo orderPo = orderService.getOne(new LambdaQueryWrapper<OrderPo>().eq(StringUtils.isNotBlank(orderNo),OrderPo::getOrderNo ,orderNo));
        if(Objects.isNull(orderPo)) {
            throw new WashException("订单号不正确:"+ orderNo);
        }

        //判断状态，按照道理这里要判断订单状态是不是取鞋中，但是有极端情况用户支付了 我们的系统还是待支付，所以这里暂时取消订单状态判断
        //只判断订单来源
        if(!StringUtils.equals(orderPo.getComeFrom(),"3")) {
            throw new WashException("订单来源不正确:"+ orderPo.getComeFrom());
        }
        orderPo.setDyBookId(bookId);
        return R.ok(dyRefundRequest.userCancelBookPost(orderPo));
    }


}

